package com.woniuxy.his.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.controller.form.AddGuahaoForm;
import com.woniuxy.his.entity.Guahao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.his.vo.GuaHaoVo;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 挂号表 Mapper 接口
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Repository
public interface GuahaoMapper extends BaseMapper<Guahao> {

    //模糊查询病人
    @Select("<script>select hgua.gua_id,hpat.patient_name, hdep.dep_name,hdep.dep_id,hdoc.doctor_name,hpat.patient_gender,hpat.patient_age,hpat.patient_id_card,hgua.price,hgua.guahao_add_time from his_guahao hgua join his_department hdep join his_doctor hdoc join his_patient hpat on hgua.patient_id = hpat.patient_id and hgua.dep_id=hdep.dep_id and hgua.doctor_id = hdoc.doctor_id <where> <if test='patientName!=null'> patient_name like concat('%',#{patientName},'%')</if></where></script>")
    IPage<AddGuahaoForm> findByPage(IPage<AddGuahaoForm> page, String patientName);

    //获取挂号信息，在挂号处理页面展示(挂号表，病人表，病历表，医生表，科室表)
    @Select("select hg.guahao_update_time,hg.gua_id,hg.status,hp.patient_name,hp.patient_age,hp.patient_gender,hp.patient_tel,hp.patient_id_card,hd.doctor_name,hde.dep_name,hc.symptom,hc.dia_result from his_guahao hg join his_patient hp join his_case hc join his_doctor hd join his_department hde on hg.patient_id=hp.patient_id and hp.patient_id=hc.patient_id and hg.dep_id=hde.dep_id and hg.doctor_id=hd.doctor_id and hd.doctor_id=#{doctorId} order by sort")
    public IPage<GuaHaoVo> queryInformationOfGuaHao(IPage<GuaHaoVo> page,Integer doctorId);

    //根据返回的挂号ID,将修改后的病人状态status
    @Update("update his_guahao set status=#{status},sort=#{sort}+sort where gua_id=#{guaId}")
    public Integer modifyStatus(Integer guaId,String status,Integer sort);

    //在挂号表查询该病例的病人ID
    @Select("select patient_id from his_guahao where gua_id=#{guaId}")
    public Integer queryPatientIdByGua(Integer guaId);

    //在病人表获取病例的姓名
    @Select("select patient_name from his_patient where patient_id=#{patientId}")
    public String queryPatientNameByPatient(Integer patientId);

    //在病列表判断是否存在病人ID
    @Select("select patient_name from his_case where patient_id=#{patientId}")
    public String queryPatientNameByCase(Integer patientId);

    //病历表不存在病人信息，则添加
    @Select("insert into his_case (symptom,dia_result,patient_id,patient_name) values (#{symptom},#{diaResult},#{patient},#{patientName})")
    public Integer insertPatient(String symptom,String diaResult,Integer patientId,String patientName);


    //根据返回的挂号ID,将修改后的病人症状symptom添加数据库
    @Update("update his_case set symptom=#{symptom},dia_result=#{diaResult} where patient_id in (select patient_id from his_guahao where gua_id=#{guaId})")
    public Integer modifySymptom(Integer guaId,String symptom,String diaResult);


    //超级管理员获取挂号信息(GuaHaoMapper)
    @Select("select hg.guahao_update_time,hg.gua_id,hg.status,hp.patient_name,hp.patient_age,hp.patient_gender,hp.patient_tel,hp.patient_id_card,hd.doctor_name,hde.dep_name,hc.symptom,hc.dia_result from his_guahao hg join his_patient hp join his_case hc join his_doctor hd join his_department hde on hg.patient_id=hp.patient_id and hp.patient_id=hc.patient_id and hg.dep_id=hde.dep_id and hg.doctor_id=hd.doctor_id order by sort")
    public IPage<GuaHaoVo> queryInformationOfGuaHaoBySuper(IPage<GuaHaoVo> page);
}
